using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace P4Shelf
{
    public partial class Form1 : Form
    {
        private void createtab_name_DropDown(object sender, EventArgs e)
        {
            // populate with list of shelves
            this.createtab_name.Items.Clear();
            this.createtab_name.Items.AddRange(_ShelfGUI.Shelves());
        }

        private void createtab_job_DropDown(object sender, EventArgs e)
        {
            // populate with list of jobs
            this.createtab_job.Items.Clear();
            this.createtab_job.Items.AddRange(_ShelfGUI.Jobs());
        }

        private void createtab_job_TextChanged(object sender, EventArgs e)
        {
            _ShelfGUI.CreateTab.JobName = this.createtab_job.Text;
            _OnDataChanged_CreateTab();
        }

        private void createtab_name_TextChanged(object sender, EventArgs e)
        {
            _ShelfGUI.CreateTab.ShelfName = this.createtab_name.Text;
            _OnDataChanged_CreateTab();
        }

        private void createtab_files_SelectedIndexChanged(object sender, EventArgs e)
        {
            _OnDataChanged_CreateTab();
        }

        private void createtab_desc_TextChanged(object sender, EventArgs e)
        {
            _ShelfGUI.CreateTab.Comment = this.createtab_desc.Text;
            _OnDataChanged_CreateTab();
        }

        private void createtab_activate_Click(object sender, EventArgs e)
        {
            System.Collections.Generic.List<string> lStrings = new System.Collections.Generic.List<string>();

            foreach (string s in this.createtab_files.CheckedItems)
            {
                lStrings.Add(s);
            }

            _ShelfGUI.Shelve(lStrings.ToArray(), this._ShelfGUI.CreateTab, this.createtab_release.Checked);
            _Refresh();
        }


        private void managetab_name_TextChanged(object sender, EventArgs e)
        {
            // get the shelf's name that we're going to view
            this._ShelfGUI.ManageTab.ShelfName = this.managetab_name.Text;
            _Update_Tab2();
        }

        private void managetab_name_DropDown(object sender, EventArgs e)
        {
            // populate with list of shelves
            this.managetab_name.Items.Clear();
            this.managetab_name.Items.AddRange(_ShelfGUI.Shelves());
        }

        private void managetab_update_Click(object sender, EventArgs e)
        {
            // Update button in the manager tab
            this._ShelfGUI.ManageTab.Update();
            _Update_Tab2();
        }

        private void managetab_unshelve_Click(object sender, EventArgs e)
        {
            // Unshelf button in the manager tab
            this._ShelfGUI.ManageTab.Inflate();
            _Update_Tab2();
        }

        private void managetab_delete_Click(object sender, EventArgs e)
        {
            this._ShelfGUI.ManageTab.Delete();
            this.managetab_name.Text = "";
            _Update_Tab2();
        }

        private void managetab_desc_TextChanged(object sender, EventArgs e)
        {
            _ShelfGUI.ManageTab.Comment = this.managetab_desc.Text;
        }
        private void managetab_job_TextChanged(object sender, EventArgs e)
        {
            _ShelfGUI.ManageTab.JobName = this.managetab_job.Text;
        }
        
        private void managetab_resolve_CheckedChanged(object sender, EventArgs e)
        {
            _ShelfGUI.ManageTab.DoAutoResolve = this.managetab_resolve.Checked;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char)Keys.F5:
                    _Refresh();
                    break;
            }
        }

        private void configtab_browse_Click(object sender, EventArgs e)
        {
            DialogResult d = this.folderBrowserDialog1.ShowDialog();
            if (d == DialogResult.OK)
            {
                this.configtab_shelfdir.Text = this.folderBrowserDialog1.SelectedPath;
                _Update_Tab3();
            }
        }

        private void configtab_new_Click(object sender, EventArgs e)
        {
            this.configtab_connections.Items.Add(this._ShelfGUI.ConfigTab.AddConnection());
        }

        private void configtab_connections_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this._ShelfGUI.ConfigTab.CurrentIndex == this.configtab_connections.SelectedIndex)
            {
                return;
            }

            this._ShelfGUI.ConfigTab.CurrentIndex = this.configtab_connections.SelectedIndex;

            // Set field values to this index
            this.configtab_user.Text = this._ShelfGUI.ConfigTab.User;
            this.configtab_clientSpec.Text = this._ShelfGUI.ConfigTab.ClientSpec;
            this.configtab_depo.Text = this._ShelfGUI.ConfigTab.Depo;
            this.configtab_password.Text = this._ShelfGUI.ConfigTab.Password;
            this.configtab_confirmpassword.Text = this._ShelfGUI.ConfigTab.Password;

            if (-1 == this._ShelfGUI.ConfigTab.CurrentIndex)
            {
                this.configtab_user.Enabled = false;
                this.configtab_clientSpec.Enabled = false;
                this.configtab_depo.Enabled = false;
                this.configtab_password.Enabled = false;
                this.configtab_confirmpassword.Enabled = false;
            }
            else if (!this.configtab_user.Enabled)
            {
                this.configtab_user.Enabled = true;
                this.configtab_clientSpec.Enabled = true;
                this.configtab_depo.Enabled = true;
                this.configtab_password.Enabled = true;
                this.configtab_confirmpassword.Enabled = true;
            }

        }

        private void configtab_apply_Click(object sender, EventArgs e)
        {
            Nutils.Console.Notify("Updating Config.");
            this._ShelfGUI.ConfigTab.WriteConfig();
            this._ShelfGUI.Connect();
            this._ShelfGUI.ConfigTab.HasChanged = false;
            _Update_Tab3();
            _Refresh();
            _OnDataChanged_CreateTab();
        }

        private void configtab_user_TextChanged(object sender, EventArgs e)
        {
            this._ShelfGUI.ConfigTab.User = this.configtab_user.Text;
            _Update_Tab3();
        }

        private void configtab_clientSpec_TextChanged(object sender, EventArgs e)
        {
            this._ShelfGUI.ConfigTab.ClientSpec = this.configtab_clientSpec.Text;
            _Update_Tab3();
        }

        private void configtab_depo_TextChanged(object sender, EventArgs e)
        {
            this._ShelfGUI.ConfigTab.Depo = this.configtab_depo.Text;
            _Update_Tab3();
        }

        private void configtab_password_or_3_TextChanged(object sender, EventArgs e)
        {
            if (this.configtab_password.Text == this.configtab_confirmpassword.Text)
            {
                this._ShelfGUI.ConfigTab.Password = this.configtab_password.Text;
            }
            _Update_Tab3();
        }

        private void configtab_del_Click(object sender, EventArgs e)
        {
            if (-1 == this.configtab_connections.SelectedIndex)
            {
                return;
            }

            this._ShelfGUI.ConfigTab.DeleteConnection();
            this.configtab_connections.Items.Clear();
            this.configtab_connections.Items.AddRange(this._ShelfGUI.ConfigTab.P4Connections.ToArray());
        }

        private void configtab_connections_MouseClick(object sender, MouseEventArgs e)
        {
            configtab_connections.SelectedIndex = configtab_connections.IndexFromPoint(e.X, e.Y);
            if (e.Button != MouseButtons.Right || 0 > this._ShelfGUI.ConfigTab.CurrentIndex)
            {
                return;
            }

            contextMenuStrip1.Show(this.configtab_connections, e.X, e.Y);
        }

        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._RenameDialog = new RenameDialog(this);
            this.Enabled = false;
        }

        public void RenameSelectedConnection(string newConnectionName)
        {
            if (this._ShelfGUI.ConfigTab.ConnectionName == newConnectionName)
            {
                return;
            }
            this._ShelfGUI.ConfigTab.ConnectionName = newConnectionName;
            _Update_Tab3();
        }

        public void RenameDialogClosed()
        {
            this.SetTopLevel(true);
            this.Enabled = true;
            this._RenameDialog = null;
            this.configtab_connections.Items.Clear();
            this.configtab_connections.Items.AddRange(this._ShelfGUI.ConfigTab.P4Connections.ToArray());
            this.configtab_connections.SelectedIndex = this._ShelfGUI.ConfigTab.CurrentIndex;
        }
    }
}